/*
 * AS3 MultiTouch Client
 *
 * DragBehaviour - Moves the object to the cursor position  
 *
 * Copyright 2008 Artifacts - Fine Software Development
 * http://www.artifacts.de
 * Author: Michael Markowski (m.markowski@artifacts.de)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

/**
 * DragBehaviour - Moves the object to the cursor position 
 * Uses caurina tweener for moving.  
 */
package de.artifacts.multitouch.behaviours
{
	import de.artifacts.multitouch.Cursor;
	import de.artifacts.multitouch.TouchEvent;
	
	import flash.geom.Point;
	
	public class TwoFingerBehaviour extends TouchBehaviour
	{
		public var hotSpotX : Number;
		public var hotSpotY : Number;		
		public var touchingCursors : Array;
		public static const MIN_SCALE : Number = 0.25;
		public static const DISTANCE_SCALE_FACTOR : Number = 100;
				
		override public function touchedHandler(event : TouchEvent) : void
		{
			super.touchedHandler(event);						
			touchingCursors = getCursorsTouchingMe();
			if (this.touchingCursors.length < 2) return;
			for each (var aCursor : Cursor in this.touchingCursors)
				aCursor.shouldSurpressInterpretationAsTap = true;			
		}
		
		override public function releasedHandler(event : TouchEvent) : void
		{	
			// remove released cursor from cursors array 
			this.touchingCursors.splice(event.cursor, 1);
		}

		// Calculate Manhattan distance
		public function manhattanDistance(p1 : Point, p2 : Point) : Number
		{
			return  distance(p1.x, p2.x) + distance(p1.y, p2.y);
		}
		
		// Calculate distance from p1 to p2
		public function distance(p1 : Number, p2 : Number) : Number
		{
			return Math.max(p1,p2)-Math.min(p1,p2);
		}
		
		public function getCursorsTouchingMe() : Array
		{
			var touchingCursors : Array = new Array();
			var cursor : Cursor;
			for each (cursor in touchManager.cursors)
			{				
				if (cursor.touches(touchable))
				{
					touchingCursors.push(cursor);
				}
			}
			return touchingCursors;
		}				
		
	}
}